exports.customerOrders = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var u_id = restfulParams.u_id;

    var is_manager = restfulParams.is_manager;//是否是管理 0-否 1-是

    var apo_invite_code_condition = '';

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var limit = restfulParams.limit == undefined ? 20 : restfulParams.limit;

    var u_type = restfulParams.u_type;

    var u_osid = restfulParams.os_id;

    var salesArr = [];

    var userInStr = '';

    //查询员工
    function salesSearch(cb) {
        if (is_manager == 1 && (u_type == 1 || u_type == 2)) {

            //战略  查询战略伙伴下所有销售
            console.log("战略 || 代理");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + u_osid,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 1 && u_type == 3) {
            //销售主管  查询该销售主管下所有销售.包括自己。
            console.log("销售组长");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_leader/" + restfulParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 0 && u_type == 3) {
            //普通销售
            console.log("普通销售");
            salesArr.push({
                u_id: u_id
            });
            cb();
        } else if (u_type == 0) {
            //超管
            console.log("超管");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;
                    cb();

                }
            )
        }
    }

    // 拼接in语句查询条件
    function fixInCondition(cb) {
        for (var i = 0; i < salesArr.length; i++) {
            userInStr += salesArr[i].u_id + ","
        }
        userInStr = userInStr.substring(0, userInStr.length - 1);

        if (userInStr == '') {
            userInStr = "null";
        }

        apo_invite_code_condition = "/in/apo_invite_code-" + userInStr;


        cb()
    }

    //查询订单
    function allOrderList(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_order/" + apo_invite_code_condition + "/skip/" + skip + "/limit/" + limit,
                method: 'POST',
                data: {
                    fields: ['apo_id', 'apo_contact', 'apo_phone', 'apo_real_pay', 'apo_school_count', 'apo_no', 'apo_type', 'apo_product_image', 'apo_create_time', 'apo_openid', 'apo_status', 'apo_service_status'].join(",")
                }
            },
            function (data) {

                resultData.orderList = data.result.data;

                cb();

            }
        )
    }

    function queryForEachSchoolName(cb) {

        F.SerialEach(resultData.orderList, function (item, index, callback) {

            //查询客户信息
            function customerInfo(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_openid/" + item.apo_openid,
                        method: 'POST',
                        data: {
                            fields: ['c_campus_name'].join(",")
                        }
                    },
                    function (data) {

                        var schoolName = '';

                        if (data.result.data.length > 0) {
                            schoolName = data.result.data[0].c_campus_name;
                        }

                        item.schoolName = schoolName

                        eachCb();

                    }
                )
            }


            F.Parallel([
                customerInfo,
            ], () => {
                callback();
            })

        }, cb)

    }


    F.Serial([
        salesSearch,
        fixInCondition,
        allOrderList,
        queryForEachSchoolName,
    ], () => {
        RES.successResponse(res, resultData);
    })


}